package com.sk.weichat.ui.mucfile.adapter;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.layout.StickyLayoutHelper;
import com.sk.weichat.R;
import com.sk.weichat.ui.mucfile.bean.FileBean;
import com.sk.weichat.util.ViewPiexlUtil;

import java.util.List;

public
        /**
         * 第一级悬浮
         */
class TypeAdapter extends DelegateAdapter.Adapter {

    public List<FileBean> getData() {
        return list;
    }

    private View.OnClickListener listener;
    Context context;
    List<FileBean> list;

    public void setOnTypeClickListener(View.OnClickListener listener) {
        this.listener = listener;
    }

    public TypeAdapter(Context context, List<FileBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        StickyLayoutHelper helper = new StickyLayoutHelper();
        return helper;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_media_file_type, null);
        return new TypeHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        viewHolder.itemView.setLayoutParams(new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewPiexlUtil.dp2px(context, 36)));
        if (!(viewHolder instanceof TypeHolder)) {
            return;
        }
        TypeHolder dirHolder = (TypeHolder) viewHolder;
        //箭头
        Drawable drawableLeft;
        if (list.get(i).isExpand()) {
            drawableLeft = context.getResources().getDrawable(R.mipmap.icon_group_arrow_open);
        } else {
            drawableLeft = context.getResources().getDrawable(R.mipmap.icon_group_arrow_close);
        }
        drawableLeft.setBounds(new Rect(0, 0, drawableLeft.getIntrinsicWidth(), drawableLeft.getIntrinsicHeight()));
        dirHolder.tvDir.setCompoundDrawables(drawableLeft, null, null, null);
        String dir = list.get(i).getTypeName();
        //Log.e("lzs", "dir:" + dir);
        dir = dir.substring(dir.lastIndexOf("/") + 1, dir.length());
        dirHolder.tvDir.setText(dir);
        dirHolder.tvDir.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(v);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        int size = list == null ? 0 : list.size();
        return size;
    }

    class TypeHolder extends RecyclerView.ViewHolder {
        TextView tvDir;

        public TypeHolder(@NonNull View itemView) {
            super(itemView);
            tvDir = itemView.findViewById(R.id.tv_type);
        }
    }
}